package com.atguigu.gmall.product.controller;

import java.util.concurrent.*;

/**
 * @author atguigu-mqx
 */
public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //  没有返回值！
        //        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
        //            System.out.println("hello");
        //        });
        //
        //        System.out.println(completableFuture.get());
        //
        //        //  有返回值！
        //        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
        //            return "204";
        //        });
        //        System.out.println(stringCompletableFuture.get());

        //        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
        //            int  i  = 1/0;
        //            return 1024;
        //        }).whenComplete((t,u)->{
        //            System.out.println(t+":ttttttttt");
        //            System.out.println(u+":uuuuuuuuuu");
        //        }).exceptionally((t)->{
        //            System.out.println(t+":exceptionally");
        //            return 404;
        //        });
        //
        //        System.out.println(integerCompletableFuture.get());



        //        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
        //            System.out.println("hello");
        //        }).thenAccept((t)->{
        //            System.out.println(t+"\t:tttttt");
        //        });
        //        System.out.println(completableFuture.get());

        //  串行

        //        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
        //            //  int  i  = 1/0;
        //            return 1024;
        //        }).thenApply((t)->{
        //            System.out.println(t+":\t ttttttttttttt"); // 想象1024
        //            return t*2;
        //        });
        //        System.out.println(integerCompletableFuture.get());
        //        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
        //            return "hello";
        //        });
        //        CompletableFuture<Void> completableFuture1 = completableFuture.thenAccept((t) -> {
        //            System.out.println(t+":\t word");
        //        });
        //        System.out.println(completableFuture1.get());

        //  定义一个线程池： io : 密集型 2n cpu：密集型 n+1  n = 4
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                10,
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3)
        );
        //  并行！
        CompletableFuture<String> completableFutureA = CompletableFuture.supplyAsync(() -> {
            return "hello";
        },threadPoolExecutor);

        CompletableFuture<Void> completableFutureB = completableFutureA.thenAcceptAsync((s) -> {
            delaySec(3);
            printStr(s + "word");
        },threadPoolExecutor);

        CompletableFuture<Void> completableFutureC = completableFutureA.thenAcceptAsync((s) -> {
            delaySec(1);
            printStr(s + "atguigu");
        },threadPoolExecutor);

        System.out.println(completableFutureB.get());
        System.out.println(completableFutureC.get());


    }
    //  打印方法
    private static void printStr(String s) {
        System.out.println(s);
    }
    //  睡眠
    private static void delaySec(int i) {
        try {
            Thread.sleep(i*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
